import { C_IsTry, C_ScrewColorEnum } from "../../C_Config";
import C_App from "../../core/C_App";
import C_Hole from "./C_Hole";
import { default as C_Plate, default as T_Plate } from "./C_Plate";
/** 关卡组件 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class C_Customs extends cc.Component {
    @property({ editorOnly: true })
    _check: boolean = false;

    @property({ tooltip: "检测关卡配置是否正常" })
    set check(v: boolean) {
        this._check = v;
        let cnt = 0;
        const holes = this.node.getComponentsInChildren(C_Hole);
        if (holes.length % 3) {
            Editor.log("wtf>>> 螺丝孔数量为:", holes.length)

        }
        else {
            const temp = {};
            holes.forEach(v => {
                if (!temp[v.type]) {
                    temp[v.type] = 1;
                }
                else {
                    temp[v.type]++;
                }
            })

            let tag = 0;
            for (let key in temp) {
                if (temp[key] % 3) {
                    Editor.log(`wtf>>>  type=${key}的螺丝孔数量为:${temp[key]}`);
                    tag = 1;
                }
            }
            if (!tag) {
                Editor.log("检测关卡正常");

            }
        }
    }

    get check() {
        return true;
    }

    @property(cc.Node)
    guideNode: cc.Node = null;

    _plates: T_Plate[] = []
    protected onLoad(): void {
        const t = this.node.getComponentsInChildren(T_Plate);
        t.forEach(v => {
            this._plates.push(v)
        })
        if (C_IsTry && this.guideNode) {

            this.guideNode.on(cc.Node.EventType.TOUCH_START, this.onTouchGuide, this);
            this.guideNode._touchListener.setSwallowTouches(false);
        }
    }


    protected _holes: C_Hole[];
    /** 初始化关卡 */
    initCustoms() {
        let layer = 0;
        this.node.children.forEach(v => {
            if (v.name.indexOf("layer") > -1) {
                v.children.forEach(node => {
                    const plate = node.getComponent(C_Plate);
                    plate.curIndex = layer;
                    plate.node.active = false;
                })
                layer++;
            }
        })
        const holes = this._holes = this.node.getComponentsInChildren(C_Hole);
        const groupNum = Math.ceil(holes.length / 3);
        const colors = [];
        for (let i = 0; i < groupNum; i++) {
            const color = C_App.RandomUtils.randomArray(Object.values(C_ScrewColorEnum))
            colors.push(color);
            colors.push(color);
            colors.push(color);
        }
        this.shuffle(colors);
        for (let i = holes.length - 1; i >= 0; i--) {
            const hole = holes[i];
            hole.color = colors.shift();
            hole.uId = i;
        }
        this.checkPlate();
    }

    exitCustoms() {
        this.node.destroy();
    }

    /** 打乱 */
    shuffle(t) {
        for (var e = t.length - 1; e >= 0; e--) {
            var o = Math.floor(Math.random() * (e + 1));
            var n = t[o];
            t[o] = t[e], t[e] = n;
        }
        return t;
    }

    getData() {
        const holes = this._holes;
        const temp = {};
        holes.forEach(v => {
            if (!temp[v.color]) {
                temp[v.color] = 1;
            }
            else {
                temp[v.color]++;
            }
        })
        return temp;
    }

    getTotal() {
        return this._holes.length;
    }


    getColors(cnt?: number) {
        const colors = [];
        const holes = this._holes;
        cnt = cnt || holes.length;
        for (let i = cnt - 1; i >= 0; i--) {
            const hole = holes[i];
            colors.push(hole.color)
        }
        return colors
    }

    _totalColors: number[]
    getColorsSort() {
        if (this._totalColors) {
            return this._totalColors;
        }
        const colors = this.getColors();
        const temp = {};
        const record: number[] = this._totalColors = [];
        colors.forEach(v => {
            if (temp[v]) {
                temp[v]--;
            }
            else {
                temp[v] = 2;
                record.push(v);
            }
        })
        return record;
    }

    /** 当前最高层数 */
    _curTopIndex: number;
    _timer = 1;
    cnt = 0;
    list: cc.Node[] = []
    update(dt: number) {
        this._timer -= dt;
        if (this._timer <= 0) {

            this._timer = 1;
            for (let i = this._plates.length - 1; i >= 0; i--) {
                const v = this._plates[i]
                if (v.plateActive) {
                    const result = v.checkPos();
                    if (result) {
                        // var t = Date.now();
                        v.removeCom()
                        // v.node.active = false;

                        this._plates.splice(i, 1)
                    }
                }
            }
            // if (t)
            //     console.log("wtf time1111 >", Date.now() - t);

            this.checkPlate();
            // if (t) {
            //     console.log("wtf time2222 >", Date.now() - t);
            // }

        }
    }

    checkPlate() {

        let index = 0;
        for (const plate of this._plates) {
            if (plate.curIndex > index) {
                index = plate.curIndex;
            }
        }

        if (this._curTopIndex != index) {
            this._curTopIndex = index;
            for (const plate of this._plates) {
                if (plate.curIndex >= index - 3) {
                    if (!plate.node.parent)
                        plate.node.parent = plate.content;
                    plate.node.active = true;
                    if (plate.curIndex >= index - 2) {
                        // 激活碰撞
                        plate.plateActive = true;
                    }
                    else {
                        plate.plateActive = false;
                    }
                }
                else {
                    plate.plateActive = false;
                    plate.node.removeFromParent();
                    plate.node.active = false;
                }

            }

        }

    }

    onTouchGuide() {
        this.guideNode.parent.active = false;
    }
}
